package cn.com.huzo.service.purchase.impl;

import cn.com.huzo.model.entities.purchase.PoLinesMrpFeedingEntity;
import huzofw.service.impl.GenericManagerImpl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import cn.com.huzo.service.purchase.PoLinesManager;
import cn.com.huzo.service.purchase.PoLinesMrpFeedingManager;
import cn.com.huzo.service.purchase.PoLinesMrpFeedingLinesManager;
import cn.com.huzo.repository.purchase.PoHeaderRepository;
import cn.com.huzo.repository.purchase.PoLinesRepository;
import cn.com.huzo.repository.purchase.PoLinesMrpFeedingLinesRepository;
import cn.com.huzo.repository.purchase.PoLinesMrpFeedingRepository;

/**
 * 采购订单明细--引入补料单管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("PoLinesMrpFeedingManager")
public class PoLinesMrpFeedingManagerImpl extends GenericManagerImpl<PoLinesMrpFeedingEntity, String>
        implements PoLinesMrpFeedingManager {

    @Resource(name = "PoHeaderRepository")
    private PoHeaderRepository _dao_header;

    @Resource(name = "PoLinesRepository")
    private PoLinesRepository _dao_lines;

    @Resource(name = "PoLinesMrpFeedingRepository")
    private PoLinesMrpFeedingRepository _dao_lines_mrp_feeding;

    @Resource(name = "PoLinesMrpFeedingLinesRepository")
    private PoLinesMrpFeedingLinesRepository _dao_lines_mrp_feeding_lines;

    @Resource(name = "PoLinesMrpFeedingLinesManager")
    private PoLinesMrpFeedingLinesManager _manager_lines_mrp_feeding_lines;

    @Resource(name = "PoLinesManager")
    private PoLinesManager _manager_lines;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> insertLines(Long headerId, Long updatedBy, List<Long> mrpFeedingHeaderIdList, List<String> mrpFeedingLinesIdList,
            Short separateFlagByPartOfNeed, Integer limit02, Integer limit04, String poNo) throws Exception {

        // 第一步，增加明细（引入的物料需求计划表头文件）
        List<PoLinesMrpFeedingEntity> itemList = new ArrayList();
        for (int i = 0; i < mrpFeedingHeaderIdList.size(); i++) {
            PoLinesMrpFeedingEntity instance = new PoLinesMrpFeedingEntity();
            instance.setHeaderId(headerId);
            instance.setMrpFeedingHeaderId(mrpFeedingHeaderIdList.get(i));
            instance.setCreatedBy(updatedBy);
            instance.setUpdatedBy(updatedBy);
            itemList.add(instance);
        }
        _dao_lines_mrp_feeding.batchInsert(itemList);

        // 第二步，增加明细（引入的物料需求计划明细文件，即材料需求）
        Integer rowsAffected = _dao_lines_mrp_feeding_lines.insertLines(headerId, updatedBy, mrpFeedingLinesIdList, separateFlagByPartOfNeed);

        // 第三步，插入订单明细
        if (rowsAffected > 0) {
            _dao_lines.insertByImportedMrpFeedingLines(headerId, updatedBy, mrpFeedingLinesIdList, separateFlagByPartOfNeed);
            _dao_lines.delete_for_sync_lines(headerId);

            // 审计日志
            writeAuditLog(updatedBy, "采购管理→采购订单引入补料单", "订单号：" + poNo, "引入");
        }

        // 第四步：删除没有产生订单内容的无效引入
        _dao_lines_mrp_feeding_lines.deleteInvalidRow(headerId);
        _dao_lines_mrp_feeding.deleteInvalidRow(headerId);

        // 第五步：得到返回值
        Map<String, Object> _result = new HashMap();

        Map<String, Object> criterion = new HashMap();
        criterion.put("HeaderId", headerId);    // 当前为第一页
        criterion.put("PageIndex", 1);          // 当前为第一页

        // 得到引入的调达表明细信息
        criterion.put("Limit", limit02);
        _result.put("LinesMrpFeedingList", _dao_lines_mrp_feeding.fetchByHeaderId(headerId));
        _result.put("LinesMrpFeedingLinesList", _manager_lines_mrp_feeding_lines.fetchForPage(criterion));

        // 得到新的采购订单明细
        criterion.put("Limit", limit04);
        _result.put("PoLinesList", _manager_lines.fetchForPage(criterion));
        _result.put("PoMaterialList", _dao_lines.fetchPoMaterialList(headerId));

        return _result;
    }
}
